package com.hzqy.web.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hzqy.service.content.ContentServiceBean;
import com.hzqy.service.hotel.WechatHotelService;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.PmsOrgVo;


//重点注意：此Filter模块目前业务规则只适用酒店小程序应用。

public class OrgUserFilter implements Filter {
	private static final Log LOG = LogFactory.getLog(OrgUserFilter.class);
	
	public void init(FilterConfig fConfig) throws ServletException {
		LOG.debug("查询用户和账号信息过滤器启动...");
	}
	
	/**
	 * 判断uri是否需要进入过滤器进行业务处理
	 */
	public boolean isUnfilteredUri(String uri) {
		return uri.indexOf(".do") >= 0 || uri.endsWith(".jpg") || uri.endsWith(".gif") || uri.endsWith(".js") ||
				uri.endsWith(".png") ;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = ((HttpServletRequest)request);
		HttpServletResponse resp = ((HttpServletResponse)response);
		HttpSession session = req.getSession();
		String uri = req.getRequestURI();
		if(isUnfilteredUri(uri)) { //不过滤的页面
			chain.doFilter(request, response);
		}else{
			PmsOrgVo pmsOrgVo = (PmsOrgVo)session.getAttribute("pmsOrgVo");
			if(pmsOrgVo != null){
				String serverid = request.getParameter("serverid");
				if(!"".equals(serverid)&&serverid!=null){
					CmContentVo cmContentVo = new CmContentVo();
					cmContentVo.setF_cc_name(serverid);
					cmContentVo.setF_cc_type(100);
					cmContentVo.setF_cc_status(1);
					WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext()); 
					WechatHotelService wechatHotelService = (WechatHotelService)webApplicationContext.getBean("wechatHotelService");
					List<CmContentVo> cmContentVoList = wechatHotelService.selectContent(cmContentVo);
					if(cmContentVoList.isEmpty()){
						LOG.debug("重新解析失败，未查到此业务账号："+serverid);
					}else{
						CmContentVo result = cmContentVoList.get(0);
						session.setAttribute("f_hsn_serverid", serverid);
						session.setAttribute("f_hsn_roonNum", result.getF_cc_obligate1());
						List<PmsOrgVo> pmsOrgVoList = wechatHotelService.selectOrgByContentId(result.getF_cc_id());
						if(pmsOrgVoList.isEmpty()){
							LOG.debug("未查到对应组织信息，业务账号为："+serverid);
						}
						session.setAttribute("pmsOrgVo",pmsOrgVoList.get(0));
						LOG.debug("重新解析成功，已存储信息至session，账号="+serverid+",房间号="+result.getF_cc_obligate1()+",组织id="+pmsOrgVoList.get(0).getF_po_id());
					}
				}else{
					LOG.debug("跳过重新解析，未携带业务账号");
				}
				chain.doFilter(request, response);
			}else{ //第一次登陆
				String serverid = request.getParameter("serverid");
				if(!"".equals(serverid)&&serverid!=null){
					CmContentVo cmContentVo = new CmContentVo();
					cmContentVo.setF_cc_name(serverid);
					cmContentVo.setF_cc_type(100);
					cmContentVo.setF_cc_status(1);
					WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext()); 
					WechatHotelService wechatHotelService = (WechatHotelService)webApplicationContext.getBean("wechatHotelService");
					List<CmContentVo> cmContentVoList = wechatHotelService.selectContent(cmContentVo);
					if(cmContentVoList.isEmpty()){
						LOG.debug("第一次登入，未查到此业务账号："+serverid);
					}else{
						CmContentVo result = cmContentVoList.get(0);
						session.setAttribute("f_hsn_serverid", serverid);
						session.setAttribute("f_hsn_roonNum", result.getF_cc_obligate1());
						List<PmsOrgVo> pmsOrgVoList = wechatHotelService.selectOrgByContentId(result.getF_cc_id());
						if(pmsOrgVoList.isEmpty()){
							LOG.debug("未查到对应组织信息，业务账号为："+serverid);
						}
						session.setAttribute("pmsOrgVo",pmsOrgVoList.get(0));
						LOG.debug("第一次登入，已存储信息至session，账号="+serverid+",房间号="+result.getF_cc_obligate1()+",组织id="+pmsOrgVoList.get(0).getF_po_id());
					}
					chain.doFilter(request, response); 
				}else{
					LOG.debug("第一次登入，未携带业务账号");
					chain.doFilter(request, response); 
				}
			}
		}
	}
	
	public void destroy() {
		LOG.debug("查询用户和账号信息过滤器销毁...");
	}
	
}
